package serveurs;
/**
 *  HttpServeur
 *
 *
 * @author Peloux Arnaud</a>
 * @version 1.0
 */
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;



public class HttpServeur extends Thread{

	/**
	 * initialisation du Serveur
	 */
	private GedServeur ged;
	private ServerSocket socketServeur; //socket serveur
	private Socket socketClient; //socket client
	private HttpConnexion conn; //thread de connexion
	private int numConnexion;
	private String url;
	private Executor executor = Executors.newFixedThreadPool(20);
	public HttpServeur(String url)  {
		//Ouverture du port de connexion
		this.url=url;
		this.ged= new GedServeur(url+"\\Racine\\");
		
	}
	public void run(){
	
		
		Exec.logger.info("Initialisation du serveur");
		try {
				socketServeur= new ServerSocket(80);
			
			numConnexion =0;
			Exec.logger.info("Serveur ok");
			System.out.println("Serveur ok");
			System.out.println("\n");
			while(true){

				chargementConnClient();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Exec.logger.error("erreur de connexion", e);
			
		}
		
		}
	
	public void chargementConnClient() throws IOException{
		socketClient= socketServeur.accept();
		InetAddress client =socketClient.getInetAddress();
		Exec.logger.info("Client :"+client+" est connecté");
		System.out.println("Client :"+client+" est connecté");
		conn= new  HttpConnexion(socketClient,numConnexion++,ged,url);
		executor.execute(conn);
		
	}
	
}
